package com.arvato.minipro.controller.admin.mall;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.minipro.bean.bean_do.mall.CommodityCutDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCutVo;
import com.arvato.minipro.service.mall.CommodityCutService;

/**
 * 
 * @author: smallking
 * 
 * @Descibe:砍价的Controller，处理砍价，砍价商品等
 * 其实砍价商品的倒计时，交给redis缓存来处理，更加方便
 * 
 * @Date: 2018年5月8日  上午11:24:50
 */
@Controller
@RequestMapping(value = "/admin/mall/cut")
public class AdminCutDownController {
	
	private Logger logger = LoggerFactory.getLogger(AdminCutDownController.class);

	@Autowired
	private  CommodityCutService commodityCutService;
	
	
	/**
	 * 按照条件查询多个砍价商品
	 * @param req
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData list(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		PageData data = commodityCutService.listCommodityCutByQuery(query);
		return data;
	}
	
	/**
	 * 根据ID查询商品分类
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result detail(String commodityCutId){
		Result result = new Result(true);
		CommodityCutVo commodityCutVo = commodityCutService.getCommodityCutById(commodityCutId);
		result.setData(commodityCutVo);
		return result;
	}
	
	/**
	 * 新增一个砍价商品
	 * @param commodityCutDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(CommodityCutDo commodityCutDo){
		Result result = new Result(false);
		// 新增之前 先查询当前的商品是否已经被添加到了砍价
		boolean flag = commodityCutService.checkCommodityCut(commodityCutDo.getCommodityId());
		if(flag) {
			result.setCode(203);
			result.setMsg("当前商品已经被加入砍价。");
			logger.info("当前商品已经被加入砍价。");
			return result;
		}
		commodityCutDo.setCreateTime(new Date());
		try {
			int row = commodityCutService.insert(commodityCutDo);
			if(row == 1) {
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改一个砍价商品
	 * @param req
	 * @param commodityCutDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result update(HttpServletRequest req, CommodityCutDo commodityCutDo){
		Result result = new Result(false);
		// 如果修改了砍价商品的截止日期，就要检查是否过期
		if(commodityCutDo.getEndTime() != null && new Date().getTime() > commodityCutDo.getEndTime().getTime()) {
				commodityCutDo.setStatus(2);
		}
		try {
			int row = commodityCutService.update(commodityCutDo);
			if(row == 1) {
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
